package com.example.hospitalmanagement.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.adapter.DepartmentLeftRecyclerViewAdapter
import com.example.hospitalmanagement.adapter.DepartmentRightRecyclerViewAdapter
import com.example.hospitalmanagement.bean.Data
import com.example.hospitalmanagement.bean.DeskWork
import com.example.hospitalmanagement.viewmodel.HospitalViewModel
import kotlinx.android.synthetic.main.activity_department.*

/**
 * 系统和科室二级联动Activity
 *
 * 位置:诊疗服务-预约挂号-预约挂号-同意预约须知-系统和科室二级联动
 */
class DepartmentActivity : AppCompatActivity() {
    private lateinit var viewModel: HospitalViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_department)
        initView()
    }

    /**
     * department_items_left 左边列表
     *
     * department_items_right 右边列表
     *
     * 此方法用于更新适配器，条目点击控件改变(字体颜色、大小、背景、控件的显示与隐藏)
     *      以及网络数据解析处理
     */
    private fun initView() {
        viewModel = ViewModelProviders.of(this).get(HospitalViewModel::class.java)

        edit_delete_text.isGone = true
        department_items_left.layoutManager = LinearLayoutManager(this)
        val leftRecyclerViewAdapter = DepartmentLeftRecyclerViewAdapter(this)
        val leftDataList: MutableList<Data> = mutableListOf()

        department_items_right.layoutManager = LinearLayoutManager(this)
        val rightRecyclerViewAdapter = DepartmentRightRecyclerViewAdapter(this)
        val rightDataList: MutableList<DeskWork> = mutableListOf()
        viewModel.getDepartment { list ->
            list.forEachIndexed { index, data ->
                leftDataList.add(
                    index,
                    Data(data.depSysId, data.depSysName, data.deskWorks, index == 0)
                )
            }
            leftRecyclerViewAdapter.notifyDataSetChanged()
           rightDataList.addAll(list[0].deskWorks)
            rightRecyclerViewAdapter.notifyDataSetChanged()
        }
        leftRecyclerViewAdapter.dataList = leftDataList
        department_items_left.adapter = leftRecyclerViewAdapter

        leftRecyclerViewAdapter.setListener {
            leftDataList.forEachIndexed { index, item ->
                item.checked = it == index
            }
            leftRecyclerViewAdapter.notifyDataSetChanged()
            rightDataList.clear()
            rightDataList.addAll(leftDataList[it].deskWorks)
            rightRecyclerViewAdapter.notifyDataSetChanged()
        }
        rightRecyclerViewAdapter.dataList = rightDataList
        department_items_right.adapter = rightRecyclerViewAdapter
        rightRecyclerViewAdapter.setListener { position ->
            rightDataList.forEachIndexed { index, secondaryLinkage ->
                if (index != position) {
                    false.also {it-> secondaryLinkage.checked = it }
                } else {
                    secondaryLinkage.checked = true
                }
            }
            val intent=intent
            when(intent.getIntExtra("state",-1)){
                0->{
                    val intent=Intent(this@DepartmentActivity,DeskDoctorActivity::class.java)
                    intent.putExtra("deskId",rightDataList[position].deskId)
                    Log.e("DepartmentActivity","${rightDataList[position].deskId}")
                    startActivity(intent)
                }
            }
            rightRecyclerViewAdapter.notifyDataSetChanged()
        }
        department_edit.addTextChangedListener {
            if ("" != department_edit.text.toString()) {
                edit_delete_text.isVisible = true
            } else {
                edit_delete_text.isGone = true
            }

            edit_delete_text.setOnClickListener {
                department_edit.text.clear()
            }
            viewModel.getDepartmentByName(department_edit.text.toString()) {
                leftDataList.clear()
                rightDataList.clear()
                it.forEachIndexed { index, data ->
                    if (department_edit.text.toString() != "") {
                        leftDataList.add(Data(data.depSysId, data.depSysName, data.deskWorks, true))
                        rightDataList.addAll(data.deskWorks)
                    } else {
                        leftDataList.add(
                            index,
                            Data(data.depSysId, data.depSysName, data.deskWorks, index == 0)
                        )
                        rightDataList.addAll(it[0].deskWorks)
                    }
                }
                leftRecyclerViewAdapter.notifyDataSetChanged()
                rightRecyclerViewAdapter.notifyDataSetChanged()
            }
        }
    }
}